package com.rms.server.Controller;

import com.rms.server.Domain.User;
import com.rms.server.Service.UserService;
import com.rms.server.Utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//@RestController
//@RequestMapping("/user")
//public class LoginController {
//
//
//    @Autowired
//    private UserService userService;
//
//    @PostMapping("/login")
//    @ResponseBody
//    public Map Login(@RequestBody Map<String, Object> requestData) {
//        Long ID = Long.valueOf(requestData.get("uid").toString());
//        String Password = (String) requestData.get("upassword");
//
////        System.out.println(ID);
////        System.out.println(Password);
//
//        User user = userService.findByUidAddUpassword(ID, Password);
//        HashMap<String, Object> response = new HashMap<>();
////        System.out.println(user.getUrole());
//        if (user != null) {
//            HashMap<String, Object> responseData = new HashMap<>();
//            responseData.put("token", JwtUtils.genAccessToken(String.valueOf(user.getUid())));
//            response.put("code", 20000);
//            response.put("msg", "账号密码通过验证，请求用户信息");
//            response.put("data", responseData);
//        } else {
//            response.put("code", 60204);
//            response.put("msg", "错误的账号或密码");
//        }
//        return response;
//    }
//
//    @CrossOrigin
//    @GetMapping(value = "/info")
//    @ResponseBody
//    public Map LoginInfo(@RequestParam String token) {
//        Claims claims;
//        try {
//            claims = JwtUtils.parsePayload(token);
//        } catch (Exception e) {
//            HashMap<String, Object> response = new HashMap<>();
//            response.put("code", 60204);
//            response.put("msg", "解析令牌时发生错误: " + e.getMessage());
//            return response;
//        }
//
//        if (claims == null) {
//            HashMap<String, Object> response = new HashMap<>();
//            response.put("code", 60204);
//            response.put("msg", "错误的令牌信息");
//            return response;
//        }
//
//        Long id;
//        try {
//            id = Long.parseLong(claims.get("str").toString());
//        } catch (NumberFormatException e) {
//            HashMap<String, Object> response = new HashMap<>();
//            response.put("code", 60205);
//            response.put("msg", "令牌信息格式错误");
//            return response;
//        }
//
//        User user = userService.finByUid(id);
//        if (user == null) {
//            HashMap<String, Object> response = new HashMap<>();
//            response.put("code", 60206);
//            response.put("msg", "未找到对应的用户信息");
//            return response;
//        }
//
//        HashMap<String, Object> responseData = new HashMap<>();
//        responseData.put("roles", user.getUrole());
//        responseData.put("name", user.getUname());
//        responseData.put("id", user.getUid());
//        responseData.put("avatar", user.getUavatar());
//
//        HashMap<String, Object> responseInfo = new HashMap<>();
//        responseInfo.put("code", 20000);
//        responseInfo.put("msg", "请求用户信息完成，登录成功");
//        responseInfo.put("data", responseData);
//        return responseInfo;
//    }
//
//    @PostMapping(value = "/logout")
//    @ResponseBody
//    public Map Logout() {
//        HashMap<String, Object> responseInfo = new HashMap<>();
//        responseInfo.put("code", 20000);
//        responseInfo.put("message", "退出登录成功");
//        return responseInfo;
//    }
//
//    @PostMapping("/register")
//    public Map Register(@RequestBody User user) {
//        userService.insertUser(user.getUpassword(), user.getUname(), user.getUrole(), user.getUavatar());
//        HashMap<String, Object> responseInfo = new HashMap<>();
//        responseInfo.put("code", 20000);
//        responseInfo.put("message", "注册成功");
//        return responseInfo;
//    }
//}
@Tag(name = "用户管理", description = "提供用户登录、注册和信息查询的接口")
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户登录", description = "根据用户ID和密码进行登录，返回认证令牌")
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> Login(@RequestBody Map<String, Object> requestData) {
        Long ID = Long.valueOf(requestData.get("uid").toString());
        String Password = (String) requestData.get("upassword");

        User user = userService.findByUidAddUpassword(ID, Password);
        HashMap<String, Object> response = new HashMap<>();

        if (user != null) {
            HashMap<String, Object> responseData = new HashMap<>();
            responseData.put("token", JwtUtils.genAccessToken(String.valueOf(user.getUid())));
            response.put("code", 20000);
            response.put("msg", "账号密码通过验证，请求用户信息");
            response.put("data", responseData);
        } else {
            response.put("code", 60204);
            response.put("msg", "错误的账号或密码");
        }
        return response;
    }

    @Operation(summary = "获取用户信息", description = "根据认证令牌获取用户的详细信息")
    @CrossOrigin
    @GetMapping(value = "/info")
    @ResponseBody
    public Map<String, Object> LoginInfo(
            @Parameter(description = "用户的认证令牌", required = true) @RequestParam String token) {

        Claims claims;
        try {
            claims = JwtUtils.parsePayload(token);
        } catch (Exception e) {
            HashMap<String, Object> response = new HashMap<>();
            response.put("code", 60204);
            response.put("msg", "解析令牌时发生错误: " + e.getMessage());
            return response;
        }

        if (claims == null) {
            HashMap<String, Object> response = new HashMap<>();
            response.put("code", 60204);
            response.put("msg", "错误的令牌信息");
            return response;
        }

        Long id;
        try {
            id = Long.parseLong(claims.get("str").toString());
        } catch (NumberFormatException e) {
            HashMap<String, Object> response = new HashMap<>();
            response.put("code", 60205);
            response.put("msg", "令牌信息格式错误");
            return response;
        }

        User user = userService.finByUid(id);
        if (user == null) {
            HashMap<String, Object> response = new HashMap<>();
            response.put("code", 60206);
            response.put("msg", "未找到对应的用户信息");
            return response;
        }

        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("roles", user.getUrole());
        responseData.put("name", user.getUname());
        responseData.put("id", user.getUid());
        responseData.put("avatar", user.getUavatar());

        HashMap<String, Object> responseInfo = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "请求用户信息完成，登录成功");
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    @Operation(summary = "用户退出登录", description = "使用户退出登录并返回成功消息")
    @PostMapping(value = "/logout")
    @ResponseBody
    public Map<String, Object> Logout() {
        HashMap<String, Object> responseInfo = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("message", "退出登录成功");
        return responseInfo;
    }

    @Operation(summary = "用户注册", description = "注册新用户，提供用户名、密码和角色等信息")
    @PostMapping("/register")
    public Map<String, Object> Register(@RequestBody User user) {
        userService.insertUser(user.getUpassword(), user.getUname(), user.getUrole(), user.getUavatar());
        HashMap<String, Object> responseInfo = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("message", "注册成功");
        return responseInfo;
    }
}

